package com.agile.leetcode.easy.largestTriangleArea;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 给定包含多个点的集合，从其中取三个点组成三角形，返回能组成的最大三角形的面积。
 *
 * 示例:
 * 输入: points = [[0,0],[0,1],[1,0],[0,2],[2,0]]
 * 输出: 2
 * 解释:
 * 这五个点如下图所示。组成的橙色三角形是最大的，面积为2
 *
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/largest-triangle-area
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 * @Author:ChenZhangKun
 * @Date: 2021/4/4 14:02
 */
public class LargestTriangleArea {
    public static void main(String[] args) {
        int[][] points={{0,0},{0,1},{1,0},{0,2},{2,0}};
        LargestTriangleArea area=new LargestTriangleArea();
        double v = area.largestTriangleArea(points);
        System.out.println(v);
    }

    public double largestTriangleArea(int[][] points) {
        // 将数组转为point
        List<Point> list = new ArrayList<>(10);
        // 遍历
        for (int[] point : points) {
            Point point1 = new Point();
            point1.x = point[0];
            point1.y = point[1];
            list.add(point1);
        }
        double max = 0;
        // 遍历
        for (int i = 0; i < list.size(); i++) {
            for (int j = i + 1; j < list.size(); j++) {
                for (int k = j + 1; k < list.size(); k++) {
                    // 拿到距离
                    double l1 = countDistance(list.get(i), list.get(j));
                    double l2 = countDistance(list.get(i), list.get(k));
                    double l3 = countDistance(list.get(j), list.get(k));
                    // 计算面积
                    double roundLength = (l1 + l2 + l3) / 2;
                    // 海伦公式
                    double size = Math.sqrt(roundLength * (roundLength - l1) * (roundLength - l2) * (roundLength - l3));
                    if (size > max) {
                        max = size;
                    }
                }
            }
        }
        return max;
    }


    public double countDistance(Point p1, Point p2) {
        // 拿到长度
        int x1 = p1.x;
        int y1 = p1.y;
        int x2 = p2.x;
        int y2 = p2.y;
        // 计算距离
        double sqrt = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
        return sqrt;
    }
}
